package com.unina.is2.astafantaunina.chat;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smack.packet.Presence;

import com.unina.is2.astafantaunina.activity.LoggedActivity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.StrictMode;

@SuppressLint("NewApi") public class XMPP {

	private String username;
	private String password;
	
	private String server = "54.76.27.136";
	private int port = 5222;
	
	//Pattern singleton
	private static XMPP singletonXmpp = null;
	
	private XMPP(String username, String password) {
		this.username = username;
		this.password = password;
	}
	
	public static XMPP getSingletonXMPP(String username, String password){
		if (singletonXmpp == null){
			singletonXmpp = new XMPP(username,password);
		}
		return singletonXmpp;
	}
	
	public static XMPP getSingletonXMPP(){

		return singletonXmpp;
	}
	
	@SuppressLint("NewApi") 
	protected String jID;
	private XMPPConnection connection = null; 
	
	@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") 
	public XMPPConnection connectToServer() {
		
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		ConnectionConfiguration config = new ConnectionConfiguration(server,port);
        config.setSASLAuthenticationEnabled(false);
        config.setDebuggerEnabled(false);
        
        if (connection == null) connection = new XMPPConnection(config);
        
        try{
            if (connection.isConnected()) {
            	connection.disconnect();
            	System.out.println("DISCONNESSO!!!!");
            }
            
            connection.connect();
            if (!connection.isAuthenticated()) connection.login(username, password);
            
            // Create the presence object with default availability 
            Presence presence = new Presence(Presence.Type.available);
            
            // Set the highest priority
            presence.setPriority(24);
            
            // Set available presence mode
            presence.setMode(Presence.Mode.available);
            
            // Send the presence packet through the connection
            connection.sendPacket(presence);
            
            jID = username + "@" + "fantaunina";

        }catch(XMPPException e){
            e.printStackTrace();
        }

        return connection;
	}
	
	public void sendXMPPmsg(String body,String to){
		Message message = new Message();
		message.setType(Type.chat);
		message.setTo(to);
		message.setFrom(this.jID);
		message.setBody(body);
		
		try {
			connection.sendPacket(message);
			System.out.println("MESSAGGIO INVIATO "+message.getBody()+" TO: "+message.getTo()+" FROM: "+message.getFrom());
			System.out.println("Connection "+connection);
		}catch (Exception e){
			System.out.println("PACCHETTO NON INVIATO");
			
			Intent i = new Intent();
	        i.setAction("CONNECTION_LOST");
	        LoggedActivity.getContext().sendBroadcast(i);
		
		}
		
		
	}
	
	public XMPPConnection getConnection(){
		return connection;
	}
	
}
